package com.bawei.module_home

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_mvi.base.BaseMVIActivity
import com.bawei.lib_storage.database.DBUtils
import com.bawei.lib_storage.database.entity.TakeVideoEntity
import com.bawei.module_home.databinding.ActivityHomeBinding
import com.bawei.module_home.intent.SimpleTypeIntent
import com.bawei.module_home.state.SimpleTypeState
import com.bawei.module_home.viewmodel.HomeViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class HomeActivity : BaseMVIActivity(){
    private lateinit var homeViewModel: HomeViewModel
    private val binding: ActivityHomeBinding by lazy {
        ActivityHomeBinding.inflate(layoutInflater)
    }
    override fun generateViewModel() {
        homeViewModel = ViewModelProvider(this)[HomeViewModel::class.java]
    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

    override fun initView() {
        super.initView()
//        lifecycleScope.launch {
//            repeatOnLifecycle(Lifecycle.State.STARTED){
//                withContext(Dispatchers.IO){
//                    //走mvvm
//                    val videoEntity = TakeVideoEntity(videoLocalPath = "jajaj", userid = 1)
//                    val videoEntity2 = TakeVideoEntity(videoLocalPath = "jajaj22", userid = 2)
//                    DBUtils.getDB().takeVideoDao().insertAll(videoEntity2,videoEntity)
//
//                    DBUtils.getDB().takeVideoDao().getAll().forEach {
//                        Log.d("TAG", "initView: ${it.videoLocalPath}")
//                    }
//                }
//
//            }
//        }

    }
    override fun initData() {
        super.initData()
        //发送意图
//        lifecycleScope.launch {
//            repeatOnLifecycle(Lifecycle.State.STARTED){
//                homeViewModel.intents.send(SimpleTypeIntent.getSimpleType)
//            }
//
//        }
    }

    override fun handleState() {
        //处理状态
//        lifecycleScope.launch {
//            repeatOnLifecycle(Lifecycle.State.STARTED){
//                homeViewModel._simpleTypeFlow.collect {
//                    when(it){
//                        is SimpleTypeState.Init -> {
//                            showMsg("初始化")
//                        }
//                        is SimpleTypeState.Empty -> {
//                            showMsg("空")
//                        }
//                        is SimpleTypeState.Failed -> {
//                            showMsg("失败")
//                        }
//                        is SimpleTypeState.SimpleTypeSuccess -> {
//                            showMsg("成功")
//                            Log.d("TAG", "handleState: ${it.list}")
//                        }
//                    }
//                }
//            }
//        }

    }

}